using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace Project3
{
    public class PhibesGame : Microsoft.Xna.Framework.Game
    {
        private static Random gen = new Random();
        public int Rand(int high) { return gen.Next(high); }

        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Ship flyer;
        Camera camera;
        Ground ground;
        ObjectField asteroids;
        bool fired = false;

        public Camera Camera { get { return camera; } }

        public PhibesGame()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            flyer = new Ship(this);
            ground = new Ground(this);
            camera = new Camera(graphics, flyer);
            asteroids = new ObjectField(this);
        }

        protected override void Initialize()
        {
            camera.Initialize();
            asteroids.Initialize();
            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            flyer.LoadContent(Content);
            ground.LoadContent(Content);
            asteroids.LoadContent(Content);
        }

        protected override void UnloadContent()
        {
        }

        protected override void Update(GameTime gameTime)
        {
            KeyboardState keyboardState = Keyboard.GetState();

            if (keyboardState.IsKeyDown(Keys.Escape))
            {
                this.Exit();
            }
            if (!flyer.Destroyed)
            {
                if (keyboardState.IsKeyDown(Keys.Left) || keyboardState.IsKeyDown(Keys.A))
                {
                    flyer.Azimuth += flyer.AzimuthStep;
                }
                else if (keyboardState.IsKeyDown(Keys.Right) || keyboardState.IsKeyDown(Keys.D))
                {
                    flyer.Azimuth -= flyer.AzimuthStep;
                }
                if (keyboardState.IsKeyDown(Keys.Up) || keyboardState.IsKeyDown(Keys.W))
                {
                    flyer.ThrustUp = 1;
                }
                else if (keyboardState.IsKeyDown(Keys.Down) || keyboardState.IsKeyDown(Keys.S))
                {
                    flyer.ThrustUp = -1;
                }
                else
                {
                    flyer.ThrustUp = 0;
                }
                if (keyboardState.IsKeyDown(Keys.Z))
                {
                    flyer.ThrustForward = 12;
                }
                else if (keyboardState.IsKeyDown(Keys.X))
                {
                    flyer.ThrustForward = 2;
                }
                else
                {
                    flyer.ThrustForward = 4;
                }

                if (keyboardState.IsKeyDown(Keys.Space))
                {
                    if (!fired)
                    {
                        flyer.Fire();
                        fired = true;
                    }
                }
                if (keyboardState.IsKeyUp(Keys.Space))
                {
                    fired = false;
                }
            }

            flyer.Update(gameTime);
            asteroids.Update(gameTime);
            camera.Update();

            List<Collidable> collisions = Functions.FindCollisions(flyer, flyer.Bullets, asteroids);
            foreach (Collidable item in collisions)
            {
                if (item == flyer)
                {
                    flyer.ThrustForward = 0;
                    flyer.ThrustUp = 0;
                    flyer.Destroyed = true;
                } else if (item is Bullet.Shell)
                    flyer.Bullets.Shells.Remove((Bullet.Shell)item);
                else if (item is ObjectField.Asteroid)
                    asteroids.Asteroids.Remove((ObjectField.Asteroid)item);
            }

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);

            flyer.Draw(graphics, gameTime);
            asteroids.Draw(graphics, gameTime);
            ground.Draw(graphics, gameTime);
            base.Draw(gameTime);
        }
    }
}
